#!/bin/sh -e

export DESTDIR="$1"

# Make LUA_ROOT "/usr" instead of "/usr/local"
clsed '/define LUA_ROOT/c#define LUA_ROOT "/usr/"' src/luaconf.h

# Optionally link with readline if available
r=$(cpt l -C readline 1 "")

mk() {
    make \
        MYCFLAGS="$CFLAGS -fPIC" \
        MYLDFLAGS="$LDFLAGS" \
        INSTALL_TOP="$DESTDIR/usr" \
        INSTALL_MAN="$DESTDIR/usr/share/man/man1" \
        INSTALL_DATA="cp -d" \
        "$@"
}

mk "linux${r:+-readline}"
mk install

clinst -Dm644 -s "s|@V@|${2%.*}|;s|@R@|$2|" lua.pc.in "$1/usr/lib/pkgconfig/lua.pc"
